<?php
abstract class AdnLibraryController extends Zend_Controller_Action
{
    protected static $_loadedTables = array();
    
    public function __get($prop)
    {
        $class = false;
        if (substr($prop, 0, 5) == 'table') 
		{
            $class = 'Table' . substr($prop, 5);
        }
		else if (substr($prop, 0, 6) == 'object') 
		{
            $class = 'Object' . substr($prop, 6);
        }

        if (!$class) {
            trigger_error("Access to undefined property $prop", E_USER_WARNING);
            return;
        }
        return self::loadTable($class);        
    }
    
    public static function loadTable($class)
    {
        if (array_key_exists($class, self::$_loadedTables)) 
		{
            return self::$_loadedTables[$class];
        }
        if (!class_exists($class)) 
		{
            throw new JP_Lib_Exception("Class $class not found");
        }
        self::$_loadedTables[$class] = new $class;   
        return self::$_loadedTables[$class];
    }
	
	public function toXml()
    {
        $class = explode('_', get_class($this));
        $name  = strtolower(array_pop($class));
        $xml   = new SimpleXMLElement("<$name></$name>");
        foreach ($this->_primary as $pk) 
		{
            $xml[$pk] = $this->_data[$pk];
        }
        $data = array_diff($this->_data, array_flip($this->_primary));
        foreach ($data as $key=>$val) 
		{
            $xml->$key = $val;
        }

        $dom = dom_import_simplexml($xml);
        $dom = $dom->ownerDocument;
        $dom->formatOutput = $dom->xmlStandalone = true;
        $dom->encoding = 'UTF-8';
        return $dom->saveXml();
    }
}
?>